package com.jt.demo2.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class SpringAOP {
@Pointcut("@annotation(com.jt.demo2.anno.CGB2110)")
    public void pointcut(){}
//@Before("pointcut()") AOP-前置通知
    public void before(JoinPoint joinPoint){
    //1.获取对象类型
    Class  targetClass = joinPoint.getTarget().getClass();
    //2.获取目标的路径
     String path= joinPoint.getSignature().getDeclaringTypeName();
    //3.获取目标对象的方法名称
     String methodName=joinPoint.getSignature().getName();
    //4.获取方法参数
    Object[] args = joinPoint.getArgs();
    System.out.println(targetClass);
    System.out.println(path);
    System.out.println(methodName);
    System.out.println(args);
    System.out.println(Arrays.toString(args));
}
@AfterReturning(value = "pointcut()",returning = "result") //AOP-后置通知
    public void afterReturn(JoinPoint joinPoint,Object result){
    System.out.println("我是后置通知"+result);

    }
    @AfterThrowing(value = "pointcut()" ,throwing ="exception")
    public void afterThrowing(JoinPoint joinPoint,Exception exception){
   exception.printStackTrace();
        System.out.println("我报错了");
    }
}
